package com.intelligentmeterreadingsys.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.intelligentmeterreadingsys.httpserver.HttpClientServer;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.util.DateUtil;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.XmlNameHelper;

/**
 * 定时定位服务
 * 
 * @author king
 * 
 */
public class TimingLocationService extends Service implements
		BDLocationListener {

	// 定位帮对象
	public LocationClient client = null;;

	// 定时器
	public Timer myTimer = null;

	// 定时任务
	public TimerTask myTimerTask = null;

	// 上传数据服务
	public HttpClientServer server = null;

	// XML帮助 对象
	public SharedPreferenceUtil sh = null;

	public long time;

	@Override
	public void onCreate() {
		server = new HttpClientServer(this);
		sh = new SharedPreferenceUtil(this);
		client = new LocationClient(this);

		initLocation();
		client.start();
		initTimer();

		start();
		super.onCreate();

	}

	@Override
	public void onDestroy() {
		if (myTimer != null) {
			myTimerTask.cancel();
			myTimer.cancel();
		}
		if (client != null && client.isStarted()) {
			client.stop();
		}
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

	/**
	 * 开启定时器
	 */
	public void start() {
		myTimer.schedule(myTimerTask, 1000, 180000);
	}

	/**
	 * 初始化定时器及任务
	 */
	public void initTimer() {
		if (myTimer == null) {
			myTimer = new Timer();
		}
		if (myTimerTask == null) {
			myTimerTask = new TimerTask() {

				@Override
				public void run() {

					UploadLocation(sh.getValue(XmlNameHelper.GPSLATITUDE),
							sh.getValue(XmlNameHelper.GPSLONGITUDE));

				}
			};
		}
	}

	/**
	 * 消息处理
	 */
	Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			if (msg.what == 0) {
				System.out.println("超时");
				Double latitude = Double.parseDouble(sh
						.getValue(XmlNameHelper.GPSLATITUDE));

				Double longitude = Double.parseDouble(sh
						.getValue(XmlNameHelper.GPSLONGITUDE));

			} else {

				if (msg.what == 1) {
					System.out.println("定位成功");
					Bundle bundle = msg.getData();
					Double latitude = bundle.getDouble("latitude");
					Double longitude = bundle.getDouble("longitude");

					sh.insertValue(XmlNameHelper.GPSLATITUDE, latitude + "");
					sh.insertValue(XmlNameHelper.GPSLONGITUDE, longitude + "");
					sh.insertValue(XmlNameHelper.GPSDATE,
							DateUtil.getBeforeDay(0));

				}
			}
		}

	};

	/**
	 * 初始化定位
	 */
	public void initLocation() {
		client = new LocationClient(this);
		LocationClientOption option = new LocationClientOption();
		// 打开GPS
		option.setOpenGps(true);
		// 设置坐标类型
		option.setCoorType("bd09ll");
		// 设置网络优先
		option.setPriority(LocationClientOption.GpsFirst);
		// 设置产品线名称
		option.setProdName("waterSys");

		// 设置定位定时时间
		option.setScanSpan(5000);
		client.setLocOption(option);
		client.registerLocationListener(this);

	}

	@Override
	public void onReceiveLocation(BDLocation location) {
		openGPSSettings();
		if (location != null) {
			Toast.makeText(TimingLocationService.this,
					location.getLocType() + "", Toast.LENGTH_SHORT).show();
			if (location.getLocType() == 61) {
				sh.insertValue(XmlNameHelper.GPSLATITUDE,
						location.getLatitude() + "");
				sh.insertValue(XmlNameHelper.GPSLONGITUDE,
						location.getLongitude() + "");

			}
		}

	}

	@Override
	public void onReceivePoi(BDLocation arg0) {
		// TODO Auto-generated method stub

	}

	/**
	 * 上传定位数据
	 * 
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public String UploadLocation(String latitude, String longitude) {

		Map<String, String> parm = new HashMap<String, String>();

		parm.put("latitude", latitude);
		parm.put("longitude", longitude);
		parm.put("imsi", sh.getValue(XmlNameHelper.IMSI));
		String response = server.doPost(parm, RequestUrl.TimingLocation);

		return response;

	}

	/**
	 * 判断GPS是否存在或开启
	 */
	public boolean openGPSSettings() {
		LocationManager alm = (LocationManager) getSystemService(this.LOCATION_SERVICE);
		if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
			return true;
		}
		Toast.makeText(this, "请开启GPS！", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(intent);// 此为设置完成后返回到获取界面
		return false;
	}

}
